package com.liby.demo1;

import android.content.Context;
import android.util.Log;
import android.util.Xml;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

/**
 * @Author: Liby
 * @PackageName: com.liby.demo1
 * @ProjectName: Demo1
 * @CreateDate: 2019/12/11 22:43
 * @Desc：
 */
public class XmlUtil {

    public static String TAG = "XmlUtil";


    public static void parseXml(Context context) {
        XmlPullParser xmlPullParser = Xml.newPullParser();
        try {
            InputStream inputStream = context.getAssets().open("custom.xml");
            //ByteArrayInputStream bis = new ByteArrayInputStream();
            xmlPullParser.setInput(inputStream, StandardCharsets.UTF_8.displayName());
            int eventType = xmlPullParser.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                String name = xmlPullParser.getName();
                switch (eventType) {
                    case XmlPullParser.START_TAG:
                        if("name".equals(name)) {
                            String area = xmlPullParser.getAttributeValue(null, "area");
                            String text = xmlPullParser.nextText();
                            Log.d(TAG, "parseXml: area:" + area + ";text:" + text);
                        } else if("age".equals(name)) {
                            String age = xmlPullParser.getAttributeValue(null, "type");
                            String s = xmlPullParser.nextText();
                            Log.d(TAG, "parseXml: age:" + age + ";nextText:" + s);
                        }
                        break;
                    case XmlPullParser.END_TAG:

                        break;
                }
                eventType = xmlPullParser.next();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        }
    }

    public static String gernerateXml() {
        try {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            XmlSerializer serializer = Xml.newSerializer();
            serializer.setOutput(outputStream, "UTF-8");
            serializer.startDocument("UTF-8", true);
            serializer.startTag(null, "students");
            for (int i = 0; i < 3; i++) {
                serializer.startTag(null, "student");
                serializer.startTag(null, "name");
                serializer.attribute(null, "area", "china");
                serializer.text("qin");
                serializer.endTag(null, "name");
                serializer.startTag(null, "age");
                serializer.attribute(null, "type", "number");
                serializer.text("18");
                serializer.endTag(null, "age");
                serializer.endTag(null, "student");
            }
            serializer.endTag(null, "students");
            serializer.endDocument();
            outputStream.flush();
            outputStream.close();

            return outputStream.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

}
